import http from './request'
import { API_PATHS } from './config'
import type {
  UserInfo,
  Category,
  Post,
  PostQueryParams,
  PostPublishParams,
  PaginationResponse,
  Conversation,
  Message,
  SystemConfig
} from '@/types'

/**
 * 认证API
 */
export const authApi = {
  /**
   * 微信登录
   */
  wechatLogin(code: string, userInfo: any) {
    return http.post<{ token: string; userInfo: UserInfo }>(API_PATHS.AUTH.WECHAT_LOGIN, {
      code,
      userInfo
    })
  },

  /**
   * 绑定手机号
   */
  bindPhone(code: string) {
    return http.post<{ phone: string }>(API_PATHS.AUTH.BIND_PHONE, { code })
  },

  /**
   * 刷新Token
   */
  refreshToken() {
    return http.post<{ token: string }>(API_PATHS.AUTH.REFRESH_TOKEN)
  }
}

/**
 * 用户API
 */
export const userApi = {
  /**
   * 获取用户信息
   */
  getProfile() {
    return http.get<UserInfo>(API_PATHS.USER.PROFILE)
  },

  /**
   * 更新用户信息
   */
  updateProfile(data: Partial<UserInfo>) {
    return http.put<UserInfo>(API_PATHS.USER.UPDATE_PROFILE, data)
  },

  /**
   * 获取用户统计
   */
  getStats() {
    return http.get(API_PATHS.USER.STATS)
  },

  /**
   * 获取我的发布
   */
  getMyPosts(params: { page: number; size: number; status?: string }) {
    return http.get<PaginationResponse<Post>>(API_PATHS.USER.POSTS, params)
  },

  /**
   * 获取我的收藏
   */
  getMyFavorites(params: { page: number; size: number }) {
    return http.get<PaginationResponse<Post>>(API_PATHS.USER.FAVORITES, params)
  }
}

/**
 * 分类API
 */
export const categoryApi = {
  /**
   * 获取分类列表
   */
  getList() {
    return http.get<{ categories: Category[] }>(API_PATHS.CATEGORY.LIST)
  },

  /**
   * 获取分类配置
   */
  getConfig(id: number) {
    return http.get(API_PATHS.CATEGORY.CONFIG.replace(':id', String(id)))
  }
}

/**
 * 信息发布API
 */
export const postApi = {
  /**
   * 获取信息列表(瀑布流)
   */
  getList(params: PostQueryParams) {
    return http.get<PaginationResponse<Post>>(API_PATHS.POST.LIST, params)
  },

  /**
   * 搜索信息
   */
  search(params: PostQueryParams & { keyword: string }) {
    return http.get<PaginationResponse<Post>>(API_PATHS.POST.SEARCH, params)
  },

  /**
   * 获取信息详情
   */
  getDetail(id: number) {
    return http.get<{ post: Post; relatedPosts: Post[] }>(
      API_PATHS.POST.DETAIL.replace(':id', String(id))
    )
  },

  /**
   * 发布信息
   */
  create(data: PostPublishParams) {
    return http.post<{ postId: number; status: string; message: string }>(API_PATHS.POST.CREATE, data)
  },

  /**
   * 更新信息
   */
  update(id: number, data: Partial<PostPublishParams>) {
    return http.put(API_PATHS.POST.UPDATE.replace(':id', String(id)), data)
  },

  /**
   * 删除信息
   */
  delete(id: number) {
    return http.delete(API_PATHS.POST.DELETE.replace(':id', String(id)))
  },

  /**
   * 获取发布限制
   */
  getLimit() {
    return http.get(API_PATHS.POST.LIMIT)
  },

  /**
   * 记录浏览
   */
  recordView(id: number) {
    return http.post(API_PATHS.POST.VIEW.replace(':id', String(id)))
  },

  /**
   * 点赞
   */
  like(id: number) {
    return http.post(API_PATHS.POST.LIKE.replace(':id', String(id)))
  },

  /**
   * 取消点赞
   */
  unlike(id: number) {
    return http.delete(API_PATHS.POST.LIKE.replace(':id', String(id)))
  },

  /**
   * 收藏
   */
  favorite(id: number) {
    return http.post(API_PATHS.POST.FAVORITE.replace(':id', String(id)))
  },

  /**
   * 取消收藏
   */
  unfavorite(id: number) {
    return http.delete(API_PATHS.POST.FAVORITE.replace(':id', String(id)))
  }
}

/**
 * 消息API
 */
export const messageApi = {
  /**
   * 获取会话列表
   */
  getConversations(params: { page: number; size: number }) {
    return http.get<{ conversations: Conversation[] }>(API_PATHS.MESSAGE.CONVERSATIONS, params)
  },

  /**
   * 获取聊天记录
   */
  getMessages(conversationId: number, params: { page: number; size: number; before?: string }) {
    return http.get<{ messages: Message[]; hasMore: boolean }>(
      API_PATHS.MESSAGE.MESSAGES.replace(':id', String(conversationId)),
      params
    )
  },

  /**
   * 发送消息
   */
  sendMessage(conversationId: number, data: {
    contentType: string
    content?: string
    mediaUrl?: string
    locationInfo?: any
  }) {
    return http.post<{ messageId: number; createdAt: string }>(
      API_PATHS.MESSAGE.SEND.replace(':id', String(conversationId)),
      data
    )
  },

  /**
   * 创建会话
   */
  createConversation(data: {
    targetUserId: number
    postId?: number
    firstMessage?: string
  }) {
    return http.post<{ conversationId: number }>(API_PATHS.MESSAGE.CREATE_CONVERSATION, data)
  },

  /**
   * 标记已读
   */
  markRead(conversationId: number) {
    return http.put(API_PATHS.MESSAGE.MARK_READ.replace(':id', String(conversationId)))
  }
}

/**
 * 举报API
 */
export const reportApi = {
  /**
   * 提交举报
   */
  create(data: {
    targetType: string
    targetId: number
    reasonType: string
    reasonDetail: string
    evidence?: string[]
  }) {
    return http.post(API_PATHS.REPORT.CREATE, data)
  }
}

/**
 * 系统配置API
 */
export const systemApi = {
  /**
   * 获取系统配置
   */
  getConfigs() {
    return http.get<SystemConfig>(API_PATHS.SYSTEM.CONFIGS)
  }
}

/**
 * 文件上传API
 */
export const uploadApi = {
  /**
   * 上传图片
   */
  uploadImage(filePath: string) {
    return http.upload(filePath, 'file')
  },

  /**
   * 上传视频
   */
  uploadVideo(filePath: string) {
    return http.upload(filePath, 'file')
  },

  /**
   * 上传语音
   */
  uploadVoice(filePath: string) {
    return http.upload(filePath, 'file')
  },

  /**
   * 获取云存储上传凭证
   */
  getUploadToken(fileType: 'image' | 'video' | 'voice') {
    return http.get<{ token: string; uploadUrl: string }>(API_PATHS.UPLOAD.GET_TOKEN, {
      fileType
    })
  }
}
